package com.mrobuy.jd.apply;

import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.request.supplier.VcItemProductApplyDeleteRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductApplyDeleteResponse;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.type.ApplyState;
import com.mrobuy.jdvc.entity.VCProductApply;

import java.util.List;

public class VCProductApplyDelete {

    public static void main(String[] args) throws Exception {

        List<VCProductApply> productApplies = SQLExecutor.executeQueryBeanList("select * from VC_PRODUCT_APPLY where STATE = ? and ZT = 1", VCProductApply.class, ApplyState.STATE_303.code());
        while ( productApplies!=null && productApplies.size()>0 ) {
            for (VCProductApply productApply : productApplies) {
                try {
                    delete(productApply.getApplyId(), JdClientFactory.jdClient());
                    SQLExecutor.delete(productApply);

                    SQLExecutor.execute("delete from VC_AUDIT_INFO where APPLYID = ? ", productApply.getApplyId());
                    SQLExecutor.execute("delete from VC_AUDIT_RECORD where APPLYID = ?", productApply.getApplyId());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            productApplies = SQLExecutor.executeQueryBeanList("select * from VC_PRODUCT_APPLY where STATE = ? and ZT = 1", VCProductApply.class, ApplyState.STATE_303.code());
        }

    }

    public static boolean delete(String applyId, JdClient client) throws Exception {

        VcItemProductApplyDeleteRequest request=new VcItemProductApplyDeleteRequest();
        request.setApplyId(applyId);
        VcItemProductApplyDeleteResponse response= client.execute(request);

        System.out.println(new Gson().toJson(response.getJosResultDto()));
        if ( response!=null && response.getJosResultDto().getIsSuccess() ) {
            return ( response.getJosResultDto().getSingleObj()!=null && response.getJosResultDto().getSingleObj()==1 );
        }
        return false;

    }


}
